package net.wanho.controller;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.wanho.entity.Student;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.IndexOperations;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * Author：汤小洋
 * Date：2024-11-04 15:25
 * Description：<描述>
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/es")
@Slf4j
public class IndexController {

    private final ElasticsearchRestTemplate restTemplate;

    @RequestMapping("/createIndex")
    public String createIndex(){
        IndexOperations indexOperations = restTemplate.indexOps(Student.class);
        indexOperations.create();
        return "success";
    }

    @RequestMapping("/createDoc")
    public String createDoc(){
        Student stu = new Student(1, "tang", 18, "男", "南京大学");
        restTemplate.save(stu);
        return "success";
    }

    @RequestMapping("/createBatch")
    public String createBatch(){
        List<Student> list = new ArrayList<>();
        list.add(new Student(1001, "张三", 18, "男", "北京大学"));
        list.add(new Student(1002, "李四", 19, "男", "清华大学"));
        list.add(new Student(1003, "王五", 20, "男", "浙江大学"));
        restTemplate.save(list);
        return "success";
    }

    @RequestMapping("/{id}")
    public Student findById(@PathVariable String id){
        Student stu = restTemplate.get(id, Student.class);

        log.info("这是info级别的日志。。。。。。。");

        log.error("执行indexController的findById方法出现错误。。。。。。");

        return stu;
    }
}
